package control;

import database.Database;
import java.sql.SQLException;
import model.GraveYard;

/**
 * Denne klasses opgave er at manipulere i databasen i forbindelse med kirkegårde
 * @author Sonny Sandberg
 */
public abstract class GraveYardControl 
{
    /**
     * Tilføj kirkegård
     * @param GraveYard
     * @return int
     * @throws SQLException
     * @throws ClassNotFoundException 
     */
    public static int addGraveYard(GraveYard graveYard) throws SQLException, ClassNotFoundException
    {
        int added = 0;
        Database db = new Database();
        
        // Hvis adressen der skal forbindes til kirkegården ikke allerede eksisterer, tilføj den
        if (graveYard.getAddress().getId() == 0)
        {
            AddressControl.addAddress(graveYard.getAddress());
            
            // Hent opdateret adresseobjekt, nu med ID.
            graveYard.setAddress(model.handlers.AddressHandler.getExistingAddress(graveYard.getAddress()));
        }
        
        if (graveYard.getAddress().getId() > 0)
        {
            added = db.manipulateDB("INSERT INTO bs_grave_yard (name, fk_address_id) VALUES ('"+graveYard.getName()+"', '"+graveYard.getAddress().getId()+"')");
        }
        
        db.closeDB();
        
        return added;        
    }
}